﻿using EasyNet.Componets.Core.Caching;
using EasyNet.Componets.Core.Inject;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Utils
{
 
    /// <summary>
    /// 缓存辅助类
    /// </summary>
    public static class CacheUtil
    {
        private static ICache _instance;

        private static ICache Instance
        {
            [DebuggerStepThrough]
            get
            {
                if (_instance != null)
                {
                    return _instance;
                }
                _instance = IoC.Resolve<ICache>();

                return _instance;
            }
        }

        public static void SetInstance(ICache instance)
        {
            _instance = instance;
        }

        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public static T Get<T>(string key)
        {
            return Instance.Get<T>(key);
        }

        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public static T[] Get<T>(string[] keys)
        {
            return Instance.Get<T>(keys);
        }

        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">Data</param>
        /// <param name="cacheTimeInMinute">Cache time</param>
        public static void Set(string key, object data, int cacheTimeInMinute)
        {
            Instance.Set(key, data, cacheTimeInMinute);
        }

        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">Data</param>
        public static void Set(string key, object data)
        {
            Instance.Set(key, data);
        }

        /// <summary>
        /// Adds multiple key and object to the cache.
        /// </summary>
        /// <param name="keyValues">key value map</param>
        public static void Set(Dictionary<string, object> keyValues)
        {
            Instance.Set(keyValues);
        }

        /// <summary>
        /// Gets a value indicating whether the value associated with the specified key is cached
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>Result</returns>
        public static bool IsSet(string key)
        {
            return Instance.IsSet(key);
        }

        /// <summary>
        /// Removes the value with the specified key from the cache
        /// </summary>
        /// <param name="key">/key</param>
        public static void Remove(string key)
        {
            Instance.Remove(key);
        }

        /// <summary>
        /// Clear all cache data
        /// </summary>
        public static void Clear()
        {
            Instance.Clear();
        }
    }
}
